﻿// 大二数据结构课程设计.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include "string"
#include "Book.h"
#include "TotalSystem.h"
#include "CollectSpace.h"
#include "shoppingCart.h"

int judgeInt() {
	string ID;
	int counterid, idnew;
	cin >> ID;

	counterid = 0;
	for (int i = 0; i < ID.length(); i++)
	{
		if ((ID.at(i) >= 'a' && ID.at(i) <= 'z') || (ID.at(i) >= 'A' && ID.at(i) <= 'Z'))
		{
			cout << "\n输入错误，请输入数字\n\n";
			counterid++;
			break;
		}
	}
	if (counterid != 0) {
		idnew = 0;
		return idnew;
	}
	idnew = atoi(ID.c_str()); // 转换为数值
	return idnew;
}

// 执行查询操作
void search(TotalSystem Tree) {
	int idnew;
	string findName, findAuthor;
	cout << "\n1.图书号 2.图书名称 3.作者\n请输入你想通过什么来查找：";
	int searchIndex;
	cin >> searchIndex;
	switch (searchIndex)
	{
	case 1:
		cout << "请输入你想查询的图书号：";
		idnew = judgeInt();
		if (idnew == 0) {
			break;
		}
		Tree.find(idnew); // 查找图书（图书号）
		break;
	case 2:
		cout << "请输入你想查询的图书名称：";
		cin >> findName;
		Tree.findInName(findName);
		break;
	case 3:
		cout << "请输入你想查询的作者：";
		cin >> findAuthor;
		Tree.findInAuthor(findAuthor);
		break;
	default:
		cout << "\n输入的操作错误，请重新输入\n" << endl;
		break;
	}
}

// 书库管理系统
void bookManage(int operation, TotalSystem &Tree) {
	int idnew;
	while (1)
	{
		cout << "\n1.查询 2.增加图书 3.批量增加图书 4.删除图书 5.批量删除图书 6.显示所有图书 7.返回主菜单\n\n请选择你想进行的操作：";
		int operation;
		cin >> operation; // 要进行的操作
		switch (operation)
		{
		case 1:
			search(Tree); // 查询操作
			break;
		case 2:
			Tree.addOne(); // 添加单本书
			break;
		case 3:
			Tree.addMore(); // 批量增加书籍
			break;
		case 4:
			cout << "\n请输入要删除的图书号：";
			idnew = judgeInt();
			if (idnew == 0) {
				break;
			}
			Tree.remove(idnew); // 删除图书（图书号）
			break;
		case 5:
			Tree.removeMore(); // 批量删除图书
			break;
		case 6:
			cout << endl;
			Tree.show();
			break;
		case 7:
			break;
		default:
			cout << "\n输入的操作错误，请重新输入\n" << endl;
			break;
		}
		if (operation == 7) {
			cout << endl;
			break;
		}
	}
}

// 收藏夹系统
void collectSystem(int operation, shoppingCart &shopTree, CollectSpace &collect, TotalSystem Tree) {
	while (1)
	{
		cout << "\n1.添加图书 2.查询图书 3.按价格显示图书 4.按日期显示图书 5.删除图书 6.添加至购物车 7.查看收藏夹所有图书 8.返回主菜单\n\n请选择你想进行的操作：";
		int operation, cYear, cMonth, cDay;
		int idnew, counterid = 0;
		string id;
		Book *p;
		cin >> operation; // 要进行的操作
		switch (operation)
		{
		case 1:
			cout << "请输入要添加图书的图书号：";//添加图书
			idnew = judgeInt();
			p = Tree.FindNode(idnew); // 查找相应的节点
			// 判断是否找到书籍
			if (p == NULL) {
				break;
			}
			cout << "当前日期(用空格分开)：";
			cin >> cYear >> cMonth >> cDay; // 输入时间
			if (cYear > 1900 && cYear < 2020)
			{
				if (cMonth == 2)
				{
					if (cDay >= 1 && cDay < 29)
						p->setCollect(cYear, cMonth, cDay);
					else
					{
						cout << "\n输入的时间错误\n\n";
						break;
					}
				}
				else if (cMonth == 1 || cMonth == 3 || cMonth == 5 || cMonth == 7 || cMonth == 8 || cMonth == 10 || cMonth == 12)
				{
					if (cDay >= 1 && cDay < 32)
						p->setCollect(cYear, cMonth, cDay);
					else
					{
						cout << "\n输入的时间错误\n\n";
						break;
					}
				}
				else if (cMonth == 4 || cMonth == 6 || cMonth == 9 || cMonth == 11)
				{
					if (cDay >= 1 && cDay < 31)
						p->setCollect(cYear, cMonth, cDay);
					else
					{
						cout << "\n输入的时间错误\n\n";
						break;
					}
				}
				else
				{
					cout << "\n输入的时间错误\n\n";
					break;
				}
			}
			else
			{
				cout << "\n输入的时间错误\n\n";
				break;
			}

			collect.collectID(p);
			collect.display(p);
			break;
		case 2:
			cout << "请输入要查询图书的图书号：";
			idnew = judgeInt();
			collect.searchID(idnew); // 查找相应的节点

			break;
		case 3:
			collect.showInPrice();// 按价格显示图书
			break;
		case 4:
			collect.showInDate();// 按日期显示图书
			break;
		case 5:
			cout << "请输入要删除图书的图书号：";// 删除图书
			idnew = judgeInt();
			collect.remove(idnew); // 进行操作
			break;
		case 6:
			cout << endl;//添加至购物车
			collect.addShoppingCart(shopTree);
			break;
		case 7:
			cout << endl;
			collect.inOrder();
			break;
		case 8:
			break;
		default:
			cout << "\n输入的操作错误，请重新输入\n" << endl;
			break;
		}
		if (operation == 8) {
			cout << endl;
			break;
		}
	}
}

// 购物车系统
void shoppingSystem(int operation, shoppingCart &shopTree, TotalSystem Tree, CollectSpace &collect) {
	string key;
	int counter = 0;
	Book *p;
	string findID;
	while (1)
	{
		cout << "1.添加 2.查询 3.修改购买数量 4.删除图书并加入收藏夹 5.直接删除指定图书 6.显示 7.退出\n请选择你想进行的操作：";
		int operation, shopYear, shopMonth, shopDate;
		int idnew, counterid = 0;
		string id;
		cin >> operation; // 要进行的操作
		switch (operation)
		{
		case 1:
			cout << "输入添加图书的图书号：";
			idnew = judgeInt();
			p = Tree.FindNode(idnew); // 查找相应的节点
			// 判断是否找到书籍
			if (p == NULL) {
				break;
			}
			cout << "请输入加入购物车的日期(用空格分开)：";
			cin >> shopYear >> shopMonth >> shopDate; // 输入时间
			if (shopYear > 1900 && shopYear < 2020)
			{
				if (shopMonth == 2)
				{
					if (shopDate >= 1 && shopDate < 29)
						p->setShopTime(shopYear, shopMonth, shopDate);
					else
					{
						cout << "\n输入的时间错误\n\n";
						break;
					}
				}
				else if (shopMonth == 1 || shopMonth == 3 || shopMonth == 5 || shopMonth == 7 || shopMonth == 8 || shopMonth == 10 || shopMonth == 12)
				{
					if (shopDate >= 1 && shopDate < 32)
						p->setShopTime(shopYear, shopMonth, shopDate);
					else
					{
						cout << "\n输入的时间错误\n\n";
						break;
					}
				}
				else if (shopMonth == 4 || shopMonth == 6 || shopMonth == 9 || shopMonth == 11)
				{
					if (shopDate >= 1 && shopDate < 31)
						p->setShopTime(shopYear, shopMonth, shopDate);
					else
					{
						cout << "\n输入的时间错误\n\n";
						break;
					}
				}
				else
				{
					cout << "\n输入的时间错误\n\n";
					break;
				}
			}
			else
			{
				cout << "\n输入的时间错误\n\n";
				break;
			}

			shopTree.add(p);
			break;
		case 2:
			// 查询操作
			cout << "请输入你想查询的图书号：";
			idnew = judgeInt();
			shopTree.search(idnew); // 进行操作
			break;
		case 3:
			cout << "输入需要修改数量的书的图书号:";
			idnew = judgeInt();

			// 判断书库中是否有该书籍
			shopTree.preOrder(shopTree.GetRoot(), idnew, counter);
			if (counter == 0) {
				cout << "\n购物车没有该图书\n\n";
				break;
			}

			shopTree.changeAmount(idnew); // 修改购买数量
			break;
		case 4:
			shopTree.deleteAndMoveCollectSpace(collect);
			break;
		case 5:
			shopTree.remove(); // 直接删除指定图书
			break;
		case 6:
			shopTree.displayBooks();
			shopTree.amount();
			shopTree.total();
			cout << endl;
			break;
		case 7:
			break;
		default:
			cout << "输入的操作错误，请重新输入" << endl;
			break;
		}
		if (operation == 7) break; // 当用户选择退出时，退出系统
	}
	cout << endl;
}

int main()
{
	TotalSystem Tree;
	CollectSpace collect;
	shoppingCart shopTree;
	cout << "\n欢迎进入收藏夹与购物车系统！\n\n";
	while (1)
	{
		cout << "1.图书管理系统 2.收藏夹系统 3.购物车系统 4.退出系统\n\n请选择你想进行的操作：";
		int operation;
		cin >> operation;
		switch (operation)
		{
		case 1:
			bookManage(operation, Tree);
			break;
		case 2:
			collectSystem(operation, shopTree, collect, Tree); // 收藏夹调用
			break;
		case 3:
			shoppingSystem(operation, shopTree, Tree, collect); // 购物车调用
			break;
		case 4:
			break;
		default:
			break;
		}
		if (operation == 4) {
			cout << "\n欢迎您再次使用本系统(*^▽^)/★*☆";
			break;
		}; // 当用户选择退出时，退出系统
	}
	
	cout << endl;
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门提示: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
